package com.example.pceb.controller;

import com.example.pceb.entity.HighSkillTrainingSubsidy;
import com.example.pceb.entity.AwardProjectSubsidy;
import com.example.pceb.entity.StartupSocialInsuranceSubsidy;
import com.example.pceb.service.SchoolAdminAuditService;
import com.example.pceb.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/school-admin-audit")
public class SchoolAdminAuditController {

    private static final Logger logger = LoggerFactory.getLogger(SchoolAdminAuditController.class);

    @Autowired
    private SchoolAdminAuditService schoolAdminAuditService;

    /**
     * 获取学校管理员的高技能人才培训补贴申请列表
     */
    @GetMapping("/high-skill/{username}")
    public Result getHighSkillForms(@PathVariable String username) {
        try {
            List<HighSkillTrainingSubsidy> forms = schoolAdminAuditService.getHighSkillFormsBySchoolAdmin(username);
            if (forms == null) {
                return Result.error("未找到学校管理员信息或学校信息");
            }
            return Result.success(forms);
        } catch (Exception e) {
            logger.error("获取高技能人才培训补贴申请列表失败: {}", e.getMessage());
            return Result.error("获取列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取学校管理员的获奖项目启动资金补贴申请列表
     */
    @GetMapping("/award-project/{username}")
    public Result getAwardProjectForms(@PathVariable String username) {
        try {
            List<AwardProjectSubsidy> forms = schoolAdminAuditService.getAwardProjectFormsBySchoolAdmin(username);
            if (forms == null) {
                return Result.error("未找到学校管理员信息或学校信息");
            }
            return Result.success(forms);
        } catch (Exception e) {
            logger.error("获取获奖项目启动资金补贴申请列表失败: {}", e.getMessage());
            return Result.error("获取列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取学校管理员的初创企业社会保险补贴申请列表
     */
    @GetMapping("/startup-social/{username}")
    public Result getStartupSocialForms(@PathVariable String username) {
        try {
            List<StartupSocialInsuranceSubsidy> forms = schoolAdminAuditService.getStartupSocialFormsBySchoolAdmin(username);
            if (forms == null) {
                return Result.error("未找到学校管理员信息或学校信息");
            }
            return Result.success(forms);
        } catch (Exception e) {
            logger.error("获取初创企业社会保险补贴申请列表失败: {}", e.getMessage());
            return Result.error("获取列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取学校管理员的所有申请表单统计信息
     */
    @GetMapping("/statistics/{username}")
    public Result getStatistics(@PathVariable String username) {
        try {
            List<HighSkillTrainingSubsidy> highSkillForms = schoolAdminAuditService.getHighSkillFormsBySchoolAdmin(username);
            List<AwardProjectSubsidy> awardProjectForms = schoolAdminAuditService.getAwardProjectFormsBySchoolAdmin(username);
            List<StartupSocialInsuranceSubsidy> startupSocialForms = schoolAdminAuditService.getStartupSocialFormsBySchoolAdmin(username);

            Map<String, Object> statistics = new HashMap<>();
            statistics.put("highSkillCount", highSkillForms != null ? highSkillForms.size() : 0);
            statistics.put("awardProjectCount", awardProjectForms != null ? awardProjectForms.size() : 0);
            statistics.put("startupSocialCount", startupSocialForms != null ? startupSocialForms.size() : 0);
            statistics.put("totalCount", (highSkillForms != null ? highSkillForms.size() : 0) + 
                                       (awardProjectForms != null ? awardProjectForms.size() : 0) + 
                                       (startupSocialForms != null ? startupSocialForms.size() : 0));

            return Result.success(statistics);
        } catch (Exception e) {
            logger.error("获取统计信息失败: {}", e.getMessage());
            return Result.error("获取统计信息失败: " + e.getMessage());
        }
    }

    /**
     * 审核高技能人才培训补贴申请
     */
    @PostMapping("/review/high-skill")
    public Result reviewHighSkillForm(@RequestBody Map<String, Object> request) {
        try {
            Integer id = (Integer) request.get("id");
            String status = (String) request.get("status");
            String reviewComment = (String) request.get("reviewComment");

            if (id == null || status == null) {
                return Result.error("参数不完整");
            }

            boolean success = schoolAdminAuditService.reviewHighSkillForm(id, status, reviewComment);
            if (success) {
                return Result.success("审核成功");
            } else {
                return Result.error("审核失败");
            }
        } catch (Exception e) {
            logger.error("审核高技能人才培训补贴申请失败: {}", e.getMessage());
            return Result.error("审核失败: " + e.getMessage());
        }
    }

    /**
     * 审核获奖项目启动资金补贴申请
     */
    @PostMapping("/review/award-project")
    public Result reviewAwardProjectForm(@RequestBody Map<String, Object> request) {
        try {
            Integer id = (Integer) request.get("id");
            String status = (String) request.get("status");
            String reviewComment = (String) request.get("reviewComment");

            if (id == null || status == null) {
                return Result.error("参数不完整");
            }

            boolean success = schoolAdminAuditService.reviewAwardProjectForm(id, status, reviewComment);
            if (success) {
                return Result.success("审核成功");
            } else {
                return Result.error("审核失败");
            }
        } catch (Exception e) {
            logger.error("审核获奖项目启动资金补贴申请失败: {}", e.getMessage());
            return Result.error("审核失败: " + e.getMessage());
        }
    }

    /**
     * 审核初创企业社会保险补贴申请
     */
    @PostMapping("/review/startup-social")
    public Result reviewStartupSocialForm(@RequestBody Map<String, Object> request) {
        try {
            Integer id = (Integer) request.get("id");
            String status = (String) request.get("status");
            String reviewComment = (String) request.get("reviewComment");

            if (id == null || status == null) {
                return Result.error("参数不完整");
            }

            boolean success = schoolAdminAuditService.reviewStartupSocialForm(id, status, reviewComment);
            if (success) {
                return Result.success("审核成功");
            } else {
                return Result.error("审核失败");
            }
        } catch (Exception e) {
            logger.error("审核初创企业社会保险补贴申请失败: {}", e.getMessage());
            return Result.error("审核失败: " + e.getMessage());
        }
    }
} 